﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using Models;

namespace BLL
{
    public class GetNumManager
    {
        private GetNumService objGetNumService = new GetNumService();
        #region 新增或更新
        /// <summary>
        /// 插入或更新行
        /// </summary>
        /// <param name="getNum"></param>
        /// <returns></returns>
        public int InsertNum(ref GetNum getNum)
        {
            int count = objGetNumService.GetNum(getNum.WorkParentId);
            if (count == 1)
            {
                //NumRule(ref getNum);
                objGetNumService.UpdateNum(getNum);
            }
            else
            {
                NumRule(ref getNum);
                objGetNumService.InsertNum(getNum);
            }
            return count;
        }
         /// <summary>
        /// 获取给号详情信息
        /// 需要参数：专案号 模具号
        /// </summary>
        /// <param name="zNum"></param>
        /// <param name="sapMold"></param>
        /// <returns></returns>
        public List<GetNum> GetInfo(string zNum, string sapMold)
        {
            return objGetNumService.GetInfo(zNum, sapMold);
        }

        #region 业务模块
        /// <summary>
        /// 自动给号规则
        /// </summary>
        /// <param name="getNum"></param>
        public void NumRule(ref GetNum getNum)
        {
            GetNum lastNum = objGetNumService.GetLastInfo(getNum.Business);
            string year = DateTime.Now.Year.ToString().Substring(2, 2);
            string code = objGetNumService.GetCode(getNum.Business);
            if (lastNum == null)
            {
                getNum.ZNum = code + year + "001";
                InsertSapMold(ref getNum, code);
            }
            else if (lastNum.ZNum.Substring(lastNum.ZNum.Length - 5, 2) == year)
            {
                getNum.ZNum = code + year + SerialNumber(lastNum.ZNum.Substring(lastNum.ZNum.Length - 3, 3));
                InsertSapMold(ref getNum, code);

            }
            else
            {
                getNum.ZNum = code + year + "001";
                InsertSapMold(ref getNum, code);

            }
        }
        /// <summary>
        /// 判断是否给模具号
        /// </summary>
        /// <param name="getNum"></param>
        /// <param name="code"></param>
        public void InsertSapMold(ref GetNum getNum, string code)
        {
            if (code == "M" || code == "S")
            {
                getNum.SapMold = getNum.ZNum;
            }
        }
        /// <summary>
        /// 流水号+1
        /// </summary>
        /// <param name="serialNumber"></param>
        /// <returns></returns>
        public string SerialNumber(string serialNumber)
        {
            int num = int.Parse(serialNumber);
            num++;
            return Convert.ToString(num).PadLeft(3, '0');
        }
        #endregion

        #endregion

        #region 查询
        public GetNum GetInfo(int workParentId)
        {
            return objGetNumService.GetInfo(workParentId);
        }
        #endregion
    }
}
